---
title: "Cleaning Cross-National Survey of Muslim Attitudes"
---

# Load

```{r}
# load packages
  source("helper-packages.R")

# load cross-national survey of muslim attitudes wave 1
  cnsma_1_raw <- 
    read_dta("../raw-data/y-multi-cross-national-survey-of-muslim attitudes/Wave-1/A Cross-National Survey of Muslim Attitudes Wave 1.DTA")

# load cross-national survey of muslim attitudes wave 2
  cnsma_2_raw <- 
    read_dta("../raw-data/y-multi-cross-national-survey-of-muslim attitudes/Wave-2/A Cross-National Survey of Muslim Attitudes Wave 2.DTA")
```

# Extract variables and stack the two waves

```{r}
# cross-national survey of muslim attitudes wave 1
  cnsma_1_clean <- 
    cnsma_1_raw %>% 
    select(
      M5, # date of interview: month
      M6, # date of interview: day
      M7, # date of interview: year
      Q9, # social distance question 1
      Q10, # social distance question 2
      I_AGE, # age interval
      S2, # religious preferences
      M4, # country
      D1, # R's gender
      D3A, # R's education by country
      D3B,
      D3C,
      D3D,
      D3E,
      D3F,
      D3G,
      M9, # urban/rural
      Q3G # religiosity
      ) %>% 
    mutate(
      resp_round = "Wave 1")

# cross-national survey of muslim attitudes wave 2
    cnsma_2_clean <- 
    cnsma_2_raw %>% 
    select(
      M5, # date of interview: month
      M6, # date of interview: day
      M7, # date of interview: year
      Q9, # social distance question 1
      Q10, # social distance question 2
      I_AGE, # age interval
      S2, # religious preferences
      M4, # country
      D1, # R's gender
      D3A, # R's education by country
      D3B,
      D3C,
      D3D,
      D3E,
      D3F,
      D3G,
      D3H,
      M9, # urban/rural
      Q3G # religiosity
      ) %>% 
    mutate(
      resp_round = "Wave 2")
    
# stack
    stacked_raw <-
      cnsma_1_clean %>%
      bind_rows(cnsma_2_clean)
```

# Clean the stacked data

```{r}
# clean
  stacked_clean <- 
    stacked_raw %>% 
    mutate(
      
    #########################  
    ####### META-DATA #######  
    #########################      
      
      # source name (character vector, title case)
        resp_source = "A Cross-National Survey of Muslim Attitudes",
        
      # round number (character vector, title case)  
        resp_round = resp_round,
      
      # url to dataset source, where publicly available (character vector)
        resp_original_data_url = "bit.ly/3z8Wdsm",

      # survey mode (in-person/phone/internet)
        resp_survey_mode = "phone",    

      # country (character vector; list of countries as written in original source)
        resp_country_original = 
          dplyr::recode(
            as.character(M4),
            "1" = "Senegal",
            "2" = "Niger",
            "3" = "Nigeria",
            "4" = "Malaysia",
            "5" = "United Kingdom",
            "6" = "France",
            "7" = "Germany",
            "8" = "Indonesia"),

      # country (character vector; converts to countrycode county.name list)
        resp_country_common = 
          countryname(resp_country_original),
        
      # interview date (variable of class Date; if only month given, input 1st of month)
        resp_interview_date =
          make_date(year = M7,
                    month = M5,
                    day = M6),
   
    #########################  
    ##### DEMOGRAPHICS ######  
    #########################
      
      # respondent's religion (character vector that corresponds to master list)
        resp_religion = 
          case_when(
            S2 == 1 ~ "Christian",
            S2 == 2 ~ "Muslim",
            S2 == 7 ~ "Other religion",
            S2 == 8 ~ NA_character_,
            TRUE ~ NA_character_),

      # respondent's religion (character vector that corresponds to master list)
        resp_denomination =
          case_when(
            S2 == 1 ~ "Christian (Protestant, Catholic, Orthodox Christian, other Christian)",
            S2 == 2 ~ "Muslim",
            S2 == 7 ~ "Other religion",
            S2 == 8 ~ NA_character_,
            TRUE ~ NA_character_),      
      
      # respondent's age (character vector; bins denoted by single dash ["18-25"])
        resp_age = 
          dplyr::recode(
            as.character(I_AGE),
            "1" = "18-24",
            "2" = "25-34",
            "3" = "35-44",
            "4" = "45-54",
            "5" = "55+",
            "101" = "18-24",
            "102" = "25-34",
            "103" = "35-44",
            "104" = "45-54",
            "105" = "55+"),        
      
      # respondent's education level
        resp_education_original =
          case_when(
            # United Kingdom
            D3A == 1 ~ "1. Lower elementary (1) [Primary]",
            D3A == 2 ~ "2. Upper elementary (2) [Primary]",
            D3A == 3 ~ "3. Middle school/intermediate school [Primary]",
            D3A == 4 ~ "4. Secondary [Primary]",
            D3A == 5 ~ "5. University [College]",
            D3A %in% c(98, 99) ~ NA_character_,
            # France
            D3B == 1 ~ "1. Primaire/Primary [Primary]",
            D3B == 2 ~ "2. Complémentaire/Complementary [Primary]",
            D3B == 3 ~ "3. Secondaire/Secondary [Primary]",
            D3B == 4 ~ "4. Baccalauréat/Baccalaureate [Primary]",
            D3B == 5 ~ "5. Universitaire/University [College]",
            D3B %in% c(98, 99) ~ NA_character_,
            # Germany
            D3C == 1 ~ "1. Primaire/Primary [Primary]",
            D3C == 2 ~ "2. Complémentaire/Complementary [Primary]",
            D3C == 3 ~ "3. Secondaire/Secondary [Primary]",
            D3C == 4 ~ "4. Baccalauréat/Baccalaureate [Primary]",
            D3C == 6 ~ "5. No graduation completed [Primary]",
            D3C == 5 ~ "6. Universitaire/University [College]",
            D3C == 90 ~ "0. Other",
            D3C %in% c(98, 99) ~ NA_character_,
            # Malaysia
            D3D == 1 ~ "1. No education [No education]",
            D3D == 2 ~ "2. Primary school [Primary]",
            D3D == 3 ~ "3. Lower secondary school [Primary]",
            D3D == 4 ~ "4. Upper secondary school [Primary]",
            D3D == 5 ~ "5. Technical or vocational school [Primary]",
            D3D == 6 ~ "6. Postsecondary education (matriculation, pre-university, Foundation) [Primary]",
            D3D == 7 ~ "7. Tertiary education (University, University College, polytechnic, etc.) [College]",
            D3D == 8 ~ "8. Postgraduate education [College]",
            D3D == 9 ~ "9. Sekolah Agama [Primary]", #religious educational institution
            D3D == 98 ~ NA_character_,
            # Niger
            D3E == 1 ~ "1. No education [No education]",
            D3E == 2 ~ "2. Primary school (grades 1-6) [Primary]",
            D3E == 3 ~ "3. Junior secondary school (grades 7-10) [Primary]",
            D3E == 4 ~ "4. Senior secondary school (grades 11-13) [Primary]",
            D3E == 5 ~ "5. Vocational school [Primary]",
            D3E == 6 ~ "6. Polytechnic graduate [Primary]",
            D3E == 7 ~ "7. University graduate [College]",
            D3E == 8 ~ "8. Postgraduate [College]",
            D3E == 9 ~ "9. Madrassa [Primary]", #religious educational instituion
            D3E %in% c(98, 99) ~ NA_character_,
            # Nigeria
            D3F == 1 ~ "1. No education [No education]",
            D3F == 2 ~ "2. Primary school (grades 1-6) [Primary]",
            D3F == 3 ~ "3. Junior secondary school (grades 7-9) [Primary]",
            D3F == 4 ~ "4. Senior secondary school (grades 7-9) [Primary]",
            D3F == 5 ~ "5. Vocational school [Primary]",
            D3F == 6 ~ "6. Ordinary national diploma (OND) [Primary]",
            D3F == 7 ~ "7. National certificate of education (NCE) [Primary]",
            D3F == 8 ~ "8. Polytechnic graduate [Primary]",
            D3F == 9 ~ "9. University graduate [College]", 
            D3F == 99 ~ NA_character_,
            # Senegal
            D3G == 1 ~ "1. No education [No education]",
            D3G == 2 ~ "2. Primary school (grades 1-6) [Primary]",
            D3G == 3 ~ "3. Junior secondary school (grades 7-10) [Primary]",
            D3G == 4 ~ "4. Senior secondary school (grades 11-13) [Primary]",
            D3G == 5 ~ "5. Vocational school [Primary]",
            D3G == 6 ~ "6. Polytechnic graduate [Primary]",
            D3G == 7 ~ "7. University graduate [College]",
            D3G == 8 ~ "8. Postgraduate [College]",
            D3G == 9 ~ "9. Madrassa [Primary]", #religious educational institution
            D3G %in% c(98, 99) ~ NA_character_,
            # Indonesia
            D3H == 1 ~ "1. No education [No education]",
            D3H == 2 ~ "2. Primary school [Primary]",
            D3H == 3 ~ "3. Secondary school [Primary]",
            D3H == 4 ~ "4. High school (grades 7-9) [Primary]",
            D3H == 5 ~ "5. Technical or vocational school [Primary]",
            D3H == 6 ~ "6. College or university [College]",
            D3H == 7 ~ "7. Postgraduate education [College]"),       
      
      # respondent's gender (numeric: female = 1; male = 0; other = NA)
        resp_female = 
          case_when(
            D1 == 2 ~ 1,
            D1 == 1 ~ 0,
            TRUE ~ NA_real_),

      # respondent resident in rural (vs urban) area (numeric: rural = 1; urban/semi-urban/peri-urban = 0)
        resp_rural = # significant missingness
          case_when(
            M9 == 1 ~ 0,
            M9 == 2 ~ 1),
      
    #########################  
    ### SOCIAL DISTANCE 1 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_1_qinfo = "NUM: Q9; QTEXT: Do you personally believe it is acceptable or not acceptable for a Muslim woman to marry a non-Muslim man who does not convert to Islam?; ROPTIONS: 1 = Yes, it is acceptable for a Muslim woman to marry a non-Muslim man [=0] + 2 = No, it is not acceptable for a Muslim woman to marry a non-Muslim man [=1] + 3 = In certain situations it may be acceptable [=0]; TARGET: Different religion; TYPE: Distance, family",
      
      # original response (as character vector)
        resp_soc_dist_1_original = 
          dplyr::recode(
            as.character(Q9),
            "98" = NA_character_,
            "99" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_1_bin_recode = 
          case_when(
            Q9 %in% c(1, 3) ~ 0,
            Q9 == 2 ~ 1,
            TRUE ~ NA_real_),

    #########################  
    ### SOCIAL DISTANCE 2 ###  
    #########################
    
      # original question number; question text; response options (input above)
        resp_soc_dist_2_qinfo = "NUM: Q10; QTEXT: Do you personally believe it is acceptable or not acceptable for a Muslim man to marry a non-Muslim woman who does not convert to Islam?; ROPTIONS: 1 = Yes, it is acceptable for a Muslim man to marry a non-Muslim woman [=0] + 2 = No, it is not acceptable for a Muslim man to marry a non-Muslim woman [=1] + 3 = In certain situations it may be acceptable [=0]; TARGET: Different religion; TYPE: Distance, family",
      
      # original response (as character vector)
       resp_soc_dist_2_original = 
          dplyr::recode(
            as.character(Q10),
            "98" = NA_character_,
            "99" = NA_character_),       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_soc_dist_2_bin_recode = 
          case_when(
            Q10 %in% c(1, 3) ~ 0,
            Q10 == 2 ~ 1,
            TRUE ~ NA_real_),
    
    ############################  
    ### GENERAL SOCIAL TRUST ###  
    ############################
    
      # original question number; question text; response options (input above)
        resp_gentrust_qinfo = NA, # checked; no general trust question asked

      # original response (as character vector)
        resp_gentrust_original = NA,       

      # binary recode (numeric: 1 = any negative attitude expressed; 0 otherwise)
        resp_gentrust_bin_recode = NA,    
    
    #########################  
    ##### RELIGIOSITY #######  
    #########################
    
      # original question number; question text; response options (input above)
        resp_religiosity_qinfo = "NUM: Q3G; QTEXT: How often do you use the following sources to obtain information on faith and religious issues often, sometimes, rarely, or never? Religious services or group meetings; ROPTIONS: 1 = Often + 2 = Sometimes + 3 = Rarely + 4 = Never",
  
      # original response (as numeric vector, with non-substantive responses coded as NA_real_)
        resp_religiosity_original = 
          dplyr::recode(
            as.numeric(Q3G),
            `98` = NA_real_,
            `99` = NA_real_),       

      # recode (numeric: scaled 0-1, where 1 is more religious)
        resp_religiosity_recode = (4 - resp_religiosity_original)/3
    
    ) %>% 
    select(starts_with("resp_"))
```

# Save cleaned data

```{r}
  saveRDS(stacked_clean, "../cleaned-data/y-7-multi-cross-national-muslim-attitudes.rds")
```
